/**
 * Created by 荆安喜 on 2017/9/1.
 */
$.fn.validata = function () {
    var cnmsg = {
        name:"只能包括中文、字母、数字",
        ip:'请输入合法的ip地址',
        mac:"输入合法的mac地址",
        location:"地址不能为空",
        userName:"登录用户名不能为空",
        userPassword:"登录密码不能为空",
        admin:"管理员不能为空",
        phone:"请输入合法的手机号码",
        url:"请输入合法的url地址",
        email:"请输入正确email",
        empty:"输入的值不能为空",
        port:"请输入纯数字的端口号",
        version:"设备版本不能为空"
    }
    var rules = {
        'name': function (obj) {
            var val = $(obj).val()
            var test = /^[A-Za-z\u4e00-\u9fa50-9]+$/.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.name+"</em>")
            }
        },
        'ip': function (obj) {
            var val = $(obj).val()
            var test = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.ip+"</em>")
            }
        },
        'mac': function (obj) {
            var val = $(obj).val()
            var test = /[A-Fa-f0-9]{2}[:|-][A-Fa-f0-9]{2}[:|-][A-Fa-f0-9]{2}[:|-][A-Fa-f0-9]{2}[:|-][A-Fa-f0-9]{2}[:|-][A-Fa-f0-9]{2}/.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.mac+"</em>")
            }
        },
        'location': function (obj) {
            var val = $(obj).val()
            if (val!="") {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.location+"</em>")
            }
        },
        'userName': function (obj) {
            var val = $(obj).val()
            if (val!="") {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.userName+"</em>")
            }
        },
        'userPassword': function (obj) {
            var val = $(obj).val()
            if (val!="") {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.userPassword+"</em>")
            }
        },
        'admin': function (obj) {
            var val = $(obj).val()
            if (val!="") {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.admin+"</em>")
            }
        },
        'phone': function (obj) {
            var val = $(obj).val()
            var test =/^1[34578]\d{9}$/.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.phone+"</em>")
            }
        },
        'url': function (obj) {
            var val = $(obj).val()
            var test =/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.url+"</em>")
            }
        },
        'version': function (obj) {
            var val = $(obj).val()
            if (val!="") {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.version+"</em>")
            }
        },
        'email': function (obj) {
            var val = $(obj).val()
            var test =/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.email+"</em>")
            }
        },
        'empty': function (obj) {
            var val = $(obj).val()
            var test =/\S/g.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.empty+"</em>")
            }
        },
        'port': function (obj) {
            var val = $(obj).val()
            var test =/^[0-9]*$/g.test(val)
            if (test) {
                $(obj).next().remove()
                $(obj).after("<em class='fc-success'><i class='icon-safe'></i></em>")
            } else {
                $(obj).next().remove()
                $(obj).after("<em class='R1 fc-warning'><i class='icon-security-alarm'></i>"+cnmsg.port+"</em>")
            }
        }

    }


    this.each(function (d, i) {
        $(i).blur(function () {
            var rule = $(i).attr('name')
            rules[rule](i)
        })

    })

}

